package leetcode_701_800;

public class LeeCode_768 {
    public static void main(String[] args) {
        System.out.println(maxChunksToSorted(new int[]{1, 1, 1, 0, 1, 0, 0, 0, 1, 0}));
    }
    private static int maxChunksToSorted(int[] arr) {
        int n = arr.length, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, ans = 1;
        int[] mins = new int[n], maxes = new int[n];
        for (int i = 0; i < n; i++) {
            maxes[i] = Math.max(max, arr[i]);
            max = maxes[i];
        }
        for (int i = n - 1; i >= 0; i--){
            mins[i] = Math.min(min, arr[i]);
            min = mins[i];
        }
        for (int i = 0; i < n - 1; i++) {
            ans += (maxes[i] <= mins[i + 1] ? 1 : 0);
        }
        return ans;
    }
}
